﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;
namespace BusinessComponents.Implementation
{
    class RoomTypeBLL
    {
        private RoomTypeRepository roomTypeRepository = new RoomTypeRepository();
        private BookingRepository bookingRepository = new BookingRepository();

        public void AddRoomType(RoomType roomType)
        {
            RoomTypeRepository repo = new RoomTypeRepository();
            repo.Add(roomType);
            repo.Commit();
           

            
        }

        public IEnumerable<RoomType> findAllTypes()
        {
            RoomTypeRepository repo = new RoomTypeRepository();
            return repo.GetAll();
        }

        public List<RoomType> FindAvailableRoomType(DateTime startDate, DateTime endDate, int numberOfRoom)
        {
            List<RoomType> result = new List<RoomType>();

            List<RoomType> roomTypes = roomTypeRepository.GetAll().ToList();

            foreach(RoomType roomType in roomTypes)
            {
                List<Booking> bookingList = bookingRepository.GetAll().Where(o => startDate.Date.CompareTo(o.EndDate.Value.Date) == -1
                                                && endDate.Date.CompareTo(o.StartDate.Value.Date) == 1
                                                && o.RoomType.Equals(roomType)).ToList();

                int bookedRoomTotal = 0;
                foreach (Booking booking in bookingList)
                {
                    foreach (BookingDetail bookingDetail in booking.BookingDetails)
                    {
                        bookedRoomTotal += bookingDetail.RoomCount.Value;
                    }
                }

                if (roomType.NumberOfRoom - bookedRoomTotal > numberOfRoom)
                {
                    result.Add(roomType);
                }
            }
            
            return result;
        }

        public RoomType FindByID(int id)
        {
            return roomTypeRepository.FindById(id);
        }
    }
}
